<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit;
}

include("../service/bannerService.php");
include("../service/productService.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Banner</title>
        <link rel="stylesheet" href="../css/admin-style.css" type="text/css" media="all" />
        <script src="../js/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function hideMessage() {
                var ok = document.getElementById("msgOK");
                addClass(ok, 'msg-hidden');
            }

            function addClass(el, name){
                el.className += (el.className ? ' ' : '') +name;
            }
            
            function adddBanner(){
                var form = document.getElementById("form");
                form.method = "post";
                form.action = "../action/addBannerAction.php";
                form.submit();
            }
            
            function editBanner(){
                var form = document.getElementById("form");
                form.method = "post";
                form.action = "../action/editBannerAction.php";
                form.submit();
            }
            
            function searchProducts() {
                $.ajax({
                    type: "GET",
                    url: "../action/searchProductAction.php",
                    data: "keyword=" + $("#keyword").val().trim(),
                    success: function(html){
                        $("#products").html(html);
                    }
                });
            }
        </script>
    </head>
    <body>
        <!-- Header -->
        <?php
        include("header.php");

        /* process message */
        $actionResult = $_GET["actionResult"];

        if (isset($actionResult)) {
            if ($actionResult == "ok") {
                $msg = "Cập nhật thành công!";
                $class = "msg msg-ok";
            } elseif ($actionResult == "no") {
                $msg = "Không thể cập nhật";
                $class = "msg msg-error";
            }
        }

        if ($msg == '')
            $class = 'msg msg-ok msg-hidden';
        ?>
        <!-- End Header -->

        <!-- Container -->
        <div id="container">
            <div class="shell">		
                <!-- Message OK -->		
                <div class="<?php echo $class; ?>" id="msgOK">
                    <p><strong><?php echo $msg; ?></strong></p>
                    <a href="#" class="close" onclick="hideMessage();">close</a>
                </div>
                <!-- End Message OK -->		

                <br />
                <!-- Main -->
                <div id="main">
                    <div class="cl">&nbsp;</div>

                    <!-- Content -->
                    <div id="content">        
                        <!-- Box -->
                        <div class="box">
                            <!-- Box Head -->
                            <div class="box-head">
                                <h2>Banner</h2>
                            </div>
                            <!-- End Box Head -->

                            <form id="form" method="post" action="../action/addBannerAction.php" enctype="multipart/form-data">

                                <!-- Form -->
                                <div class="form">
                                    <p>
                                        <table>
                                            <tr>
                                                <td colspan="2">
                                                    <?php
                                                    if (isset($_GET['banID'])) {
                                                        $bannerPath = getBannerPathBy($_GET['banID']);
                                                        ?>
                                                        <img src="<?php echo($bannerPath['BANNER_PATH']); ?>" width="246" heigh="421"/>
                                                        <?php
                                                    }
                                                    ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Ảnh:</td>
                                                <td>
                                                    <input type="file" name="bannerImg"/>
                                                </td>
                                            </tr>
                                        </table>
                                    </p>
                                    <p>
                                        <label>Liên Kết Đến Sản Phẩm</label>
                                        <table>
                                            <tr>
                                                <td><input type="text" class="size5" id="keyword" name="keyword"/>
                                                    <input type="button" value="Tìm Kiếm" class="button right" onclick="searchProducts();"/>
                                                </td>
                                            </tr>
                                            <tr><td>&nbsp;</td></tr>
                                            <tr>
                                                <td>
                                                    <select id="products" name="products" class="categorylist">
                                                        <option value="">Không Liên Kết</option>
                                                        <?php
                                                        $products = getAllProduct();
                                                        if (isset($_GET["banID"])) {
                                                            $thisResult = getProductByBanner($_GET["banID"]);
                                                            $thisProduct = mysql_fetch_array($thisResult);
                                                            while ($product = mysql_fetch_array($products)) {
                                                                if ($thisProduct['PRO_ID'] == $product['PRO_ID']) {
                                                                    echo "<option value='" . $product['PRO_ID'] . "' selected='selected'>" . $product["PRO_NAME"] . "</option>";
                                                                } else {
                                                                    echo "<option value='" . $product['PRO_ID'] . "'>" . $product["PRO_NAME"] . "</option>";
                                                                }
                                                            }
                                                        } else {
                                                            while ($product = mysql_fetch_array($products)) {
                                                                echo "<option value='" . $product['PRO_ID'] . "'>" . $product["PRO_NAME"] . "</option>";
                                                            }
                                                        }
                                                        ?>
                                                    </select>
                                                    <input type="hidden" name="banID"  value="<?php echo $_GET["banID"]; ?>"/>
                                                </td>
                                            </tr>
                                        </table>
                                    </p>


                                </div>
                                <!-- End Form -->

                                <!-- Form Buttons -->
                                <div class="buttons">
                                    <?php
                                    if (isset($_GET["banID"])) {
                                        ?>
                                        <input type="submit" class="button" value="Cập Nhật" onclick="editBanner();"/>
                                        <?php
                                    } else {
                                        ?>
                                        <input type="submit" class="button" value="Tạo banner" onclick="addBanner();"/>
                                        <?php
                                    }
                                    ?>
                                    <input type="button" class="button" value="Trở về" onclick="window.location = 'banner.php'"/>
                                </div>
                                <!-- End Form Buttons -->
                            </form>
                        </div>
                        <!-- End Box -->

                    </div>
                    <!-- End Content -->



                    <div class="cl">&nbsp;</div>			
                </div>
                <!-- Main -->
            </div>
        </div>
        <!-- End Container -->

        <!-- Footer -->
        <div id="footer">
            <div class="shell">
                <span class="left">&copy; 2010 - VNWeb</span>
                <span class="right">
                    Design by <a href="http://chocotemplates.com" target="_blank" title="The Sweetest CSS Templates WorldWide">VNWeb.vn</a>
                </span>
            </div>
        </div>
        <!-- End Footer -->

    </body>
</html>
